package com.seu.vcampus.view.library;

import com.seu.vcampus.client.VCampusClient;
import com.seu.vcampus.util.UIStyleUtil;
import com.seu.vcampus.vo.User;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

/**
 * 我的借阅面板
 * 显示用户当前的借阅记录
 * 
 * @author 虚拟校园开发团队
 * @since 2.2.0
 */
public class MyBorrowPanel extends JPanel {
    private VCampusClient client;
    private User currentUser;
    
    private JTable borrowTable;
    private DefaultTableModel borrowTableModel;
    private JButton refreshButton;
    private JButton returnButton;
    
    public MyBorrowPanel(VCampusClient client, User user) {
        this.client = client;
        this.currentUser = user;
        
        initializeComponents();
        setupLayout();
        setupEventHandlers();
        loadMyBorrows();
    }
    
    private void initializeComponents() {
        setLayout(new BorderLayout(15, 15));
        setBackground(UIStyleUtil.BACKGROUND_COLOR);
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        // 初始化表格
        String[] columns = {"记录ID", "图书ID", "书名", "借阅日期", "应还日期", "状态"};
        borrowTableModel = new DefaultTableModel(columns, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        borrowTable = new JTable(borrowTableModel);
        borrowTable.setFont(UIStyleUtil.BODY_FONT);
        borrowTable.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));
        borrowTable.setRowHeight(30);
        
        // 按钮
        refreshButton = UIStyleUtil.createSecondaryButton("刷新");
        returnButton = UIStyleUtil.createWarningButton("归还");
    }
    
    private void setupLayout() {
        // 按钮面板
        JPanel buttonPanel = UIStyleUtil.createCardPanel("操作");
        buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
        buttonPanel.add(refreshButton);
        buttonPanel.add(returnButton);
        
        // 表格面板
        JPanel tablePanel = UIStyleUtil.createCardPanel("当前借阅");
        tablePanel.setLayout(new BorderLayout());
        JScrollPane scrollPane = UIStyleUtil.createModernScrollPane(borrowTable);
        tablePanel.add(scrollPane, BorderLayout.CENTER);
        
        add(buttonPanel, BorderLayout.NORTH);
        add(tablePanel, BorderLayout.CENTER);
    }
    
    private void setupEventHandlers() {
        refreshButton.addActionListener(e -> loadMyBorrows());
        returnButton.addActionListener(e -> returnSelectedBook());
    }
    
    private void loadMyBorrows() {
        // 实现加载用户当前借阅记录的逻辑
        // 这里可以调用服务端接口获取用户的借阅记录
    }
    
    private void returnSelectedBook() {
        int selectedRow = borrowTable.getSelectedRow();
        if (selectedRow >= 0) {
            String recordId = (String) borrowTableModel.getValueAt(selectedRow, 0);
            String bookName = (String) borrowTableModel.getValueAt(selectedRow, 2);
            
            // 实现归还逻辑
            JOptionPane.showMessageDialog(this, 
                "归还图书: " + bookName, 
                "归还确认", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this, 
                "请先选择要归还的图书", 
                "提示", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    public void refreshData() {
        loadMyBorrows();
    }
}